package cn.lixu.org.service.impl;

import cn.lixu.basic.exception.BusinessException;
import cn.lixu.basic.service.impl.BaseServiceImpl;
import cn.lixu.basic.util.BaiduAuditUtils;
import cn.lixu.org.domain.Employee;
import cn.lixu.org.domain.Shop;
import cn.lixu.org.domain.ShopLog;
import cn.lixu.org.dto.ShopDto;
import cn.lixu.org.mapper.EmployeeMapper;
import cn.lixu.org.mapper.ShopLogMapper;
import cn.lixu.org.mapper.ShopMapper;
import cn.lixu.org.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.List;

@Service
//只读，事务传播机制=SUPPORTS，有我就用，没有事务就不用
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {
    /**
     * 商家入驻逻辑步骤：
     * 一：检验
     * 1.1.每个参数的空值判断
     * 1.2.两次密码不一致校验
     * 1.3.该店铺是否已经被入驻过
     * 二：入驻业务
     * 2.1.保存店铺管理员信息t_employee
     * 2.2.保存店铺信息t_shop
     * 2.2.将店铺的id更新到t_employee中
     **/

    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private ShopLogMapper shopLogMapper;

    //发送邮件
    @Autowired
    private JavaMailSender javaMailSender;


    @Override
    public void settlement(Shop shop) {
        //每个参数的空值判断
        if (StringUtils.isEmpty(shop.getName())
                || StringUtils.isEmpty(shop.getTel())
                || StringUtils.isEmpty(shop.getAddress())
                //  || StringUtils.isEmpty(shop.getLogo())
                || StringUtils.isEmpty(shop.getAdmin().getPhone())
                || StringUtils.isEmpty(shop.getAdmin().getEmail())
                || StringUtils.isEmpty(shop.getAdmin().getPassword())
                || StringUtils.isEmpty(shop.getAdmin().getComfirmPassword())
        ) {
            throw new BusinessException("店铺入驻参数不能为空!");
        }


        //通过百度AI审核名称是否符合要求
        Boolean aBoolean = BaiduAuditUtils.TextCensor(shop.getName());
        if (!aBoolean) {//名称有问题
            throw new BusinessException("店铺名称违规，请重新输入!");
        }

        //两次密码不一致校验
        Employee admin = shop.getAdmin();
        if (!admin.getPassword().equals(admin.getComfirmPassword())) {
            throw new BusinessException("密码错误，请重新输入");
        }

        //校验店铺是否已经被入驻过
        Shop dbshop = shopMapper.getShopByMessage(shop);
        if (dbshop != null) {
            throw new BusinessException("店铺已经入驻，请直接登陆！忘记密码请找回密码");
        }

        //保存店铺管理员信息t_employee
        employeeMapper.save(admin);


        //保存店铺信息t_shop
        shop.setAdmin_id(admin.getId());
        shopMapper.save(shop);

        //将店铺的id更新到t_employee
        admin.setShop_id(shop.getId());
        employeeMapper.update(admin);
    }


    /**
     * 审核通过
     * 1.修改状态【通过shop_id查询shop数据，再修改状态】
     * 2.录入日志 shop_log
     * 3.发送邮件
     * <p>
     * log里面可以接收shop_id和note
     */

    @Override
    public void pass(ShopLog log) throws MessagingException {//log里面可以接收shop_id和note
        //通过shop_id查询shop数据
        Shop shop = shopMapper.loadById(log.getShop_id());
        //再修改状态
        shop.setState(2);
        //更新数据库
        shopMapper.update(shop);
        //记录日志
        log.setState(2);
        log.setAudit_id(1L);
        shopLogMapper.save(log);

        //发送邮件激活
        //创建复杂邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //发送复杂邮件的工具类
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "utf-8");
        helper.setFrom("354374118@qq.com");
        helper.setSubject("店铺激活邮件");
        //log.getShop_id()
        helper.setText("<h1>你的店铺已经注册!!!</h1><img src='http://dfs.java.itsource.cn/group1/M00/00/B9/rBE3kWD_fEuALou2AAB4rV4bI7c231.jpg' ><a href='http://localhost:8080/shop/active/" + log.getShop_id() + "'>点击该链接激活</a>", true);

        //收件人
        //获取收件人
        Employee employee = employeeMapper.loadById(shop.getAdmin_id());
        //获取收件人邮件地址
        String email = employee.getEmail();
        helper.setTo("354374118@qq.com");
        //发送邮件
        javaMailSender.send(mimeMessage);
    }

    /**
     * 审核被驳回
     *
     * @param log
     * @throws MessagingException
     */
    @Override
    public void reject(ShopLog log) throws MessagingException {
        //通过shop_id查询shop数据
        Shop shop = shopMapper.loadById(log.getShop_id());
        //再修改状态
        shop.setState(4);
        //更新数据库
        shopMapper.update(shop);
        //记录日志
        log.setState(4);
        log.setAudit_id(1L);
        shopLogMapper.save(log);

        //发送邮件激活
        //创建复杂邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //发送复杂邮件的工具类
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "utf-8");
        helper.setFrom("354374118@qq.com");
        helper.setSubject("店铺审核驳回邮件");
        //log.getShop_id()
        helper.setText("<h1>你的店铺申请被驳回，请再次提交!!!</h1><img src='http://dfs.java.itsource.cn/group1/M00/00/B9/rBE3kWD_fEuALou2AAB4rV4bI7c231.jpg' ><a href='http://localhost:8080/shop/active/" + log.getShop_id() + "'>点击该链接激活</a>", true);

        //收件人
        //获取收件人【通过店铺的管理员id查询employee对象】
        Employee employee = employeeMapper.loadById(shop.getAdmin_id());
        //获取收件人邮件地址
        String email = employee.getEmail();
        helper.setTo("354374118@qq.com");
        //发送邮件
        javaMailSender.send(mimeMessage);


        /**
         * 驳回分两种情况
         * 1.修改状态  state=4，下次录入的时候是否是被驳回过
         *          如果是，就直接入驻，如果不是，再次审核
         * 2.直接删除  shop  emp
         */
    }

    @Override
    public List<ShopDto> getEcharts() {
        return shopMapper.getEcharts();
    }
}

